﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Data;
using System.Globalization;
using System.Xml.Serialization;

namespace AssignmentsManage.Converters
{
    public class NumberToPercentStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                double res = 0;
                bool worked = Double.TryParse(value.ToString(), out res);
                if (Double.IsNaN(res)) { res = 0; }
                if (worked)
                {
                    return string.Format("{0:f1}%", res);
                }
                else
                {
                    throw new FormatException("Conversion can only be from a double or integer");
                }
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException("NumberToPercentStringConverter can only be used for one way conversion.");
        }
    }
}
